package com.yao.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yao.common.exception.BusinessException;
import com.yao.sys.entity.DictTypeEntity;
import com.yao.sys.entity.dto.DictTypeDto;
import com.yao.sys.mapper.DictTypeMapper;
import com.yao.sys.service.DictTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 字典类型表 接口实现
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2019-12-09
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictTypeDto> implements DictTypeService {

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, timeout = 1, rollbackFor = BusinessException.class)
    public DictTypeEntity add(DictTypeDto entity) {
        LambdaQueryWrapper<DictTypeDto> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(null != entity.getName(), DictTypeEntity::getName, entity.getName()).eq(null != entity.getCode(),
            DictTypeEntity::getCode, entity.getCode());

        DictTypeEntity one = this.getOne(wrapper);

        if (null != one) {
            String errmsg = String.format("不能添加重复的字典类型name:%s ;; code:%s", entity.getName(), entity.getCode());
            throw new BusinessException(errmsg);
        }

        if(!this.save(entity)){
            throw new BusinessException("新增字典类型失败.");
        }

        return entity;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, timeout = 1, rollbackFor = BusinessException.class)
    public DictTypeEntity modifyById(DictTypeDto entity) {
        if (null == entity.getId()) {
            throw new BusinessException("更新的id不存在."+entity);
        }

        LambdaQueryWrapper<DictTypeDto> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(null != entity.getName(), DictTypeEntity::getName, entity.getName())
                .eq(null != entity.getCode(),DictTypeEntity::getCode, entity.getCode());
        DictTypeEntity one = this.getOne(wrapper);

        if (null != one) {
            String errmsg = String.format("已经存在相同的字典. name=%s ;; code =%s",entity.getName(), entity.getCode());
            throw new BusinessException(errmsg);
        }

        if (!this.updateById(entity)) {
            throw new BusinessException("更新字典类型失败.");
        }

        return entity;
    }

    public void listPage(){
//        PageBeanDto<DictTypeDto> pageBeanDto = new PageBeanDto<>();
//
//        LambdaQueryWrapper<DictTypeEntity> queryWrapper = Wrappers.lambdaQuery(pageBeanDto.getQuery());
//
//
//        IPage<DictTypeEntity> page = this.page(pageBeanDto.getIpage(), queryWrapper);
//
//        Console.log("分页查询：{}",page);
    }
}